package com.dpad.telematicsclientapp.presenter;


import android.util.Log;


import com.dpad.telematicsclientapp.mvp.mvp.XPresent;
import com.dpad.telematicsclientapp.netlibrary.MainApplicaton;
import com.dpad.telematicsclientapp.netlibrary.http.NetInstance;
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.TSFXEntity;
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.TSFXPhone;
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.XTSListEntity;
import com.dpad.telematicsclientapp.ui.fragment.TSFXFragment;
import com.dpad.telematicsclientapp.util.utils.ParameterUtils;

import java.util.SortedMap;
import java.util.TreeMap;


import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class TSFXPresenter extends XPresent<TSFXFragment> {
    //查询收藏列表
    public void getCollection() {
        getV().loading("正在加载...");
        SortedMap<String, Object> sortedMap = new TreeMap();
//        sortedMap.put("currentPage", "1");
//        sortedMap.put("pageSize", "50");
        sortedMap.put("vin", MainApplicaton.vin);
        NetInstance.getEventsService().tsfxmap(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getJsonBody(sortedMap)).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<TSFXEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(TSFXEntity entity) {
                        if (entity.getCode().equals("00000")) {
                            getV().showListSC(entity.getResult().getAppMapCollection(),entity.getResult().getVehLocationVo());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getV().loadingComplete();
                        getV().dealHttpError(e);
                    }

                    @Override
                    public void onComplete() {
                        getV().loadingComplete();
                    }
                });
    }



    //查询常用的电话列表
    public void getPhone() {
        getV().loading("正在加载...");
        SortedMap<String, Object> sortedMap = new TreeMap();
//        sortedMap.put("currentPage", "1");
//        sortedMap.put("pageSize", "50");
//        sortedMap.put("vin", MainApplicaton.vin);
        NetInstance.getEventsService().tsfxphone(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getJsonBody(sortedMap)).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<TSFXPhone>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(TSFXPhone entity) {
                        if (entity.getCode().equals("00000")) {
                            getV().showPhoneNumber(entity.getResult());
                        }else {//证明没有查询到电话
                            getV().NoShowPhone();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        getV().loadingComplete();
                        getV().dealHttpError(e);
                    }

                    @Override
                    public void onComplete() {
                        getV().loadingComplete();
                    }
                });
    }



    //动态显示充电桩的接口（防止审核跳转外链被拒）
    public void getxiaotieshi() {

//        getV().loading("正在加载...");
        SortedMap<String, Object> sortedMap = new TreeMap<>();


        sortedMap.put("codetype", "CHARING_INFOMATION");


        NetInstance.getEventsService2().getxiaotieshi(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getJsonBody(sortedMap)).
                subscribeOn(Schedulers.io()).
                observeOn(io.reactivex.android.schedulers.AndroidSchedulers.mainThread()).
                subscribe(new Observer<XTSListEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(XTSListEntity bean) {//回来的codeValue   为  1开启    0关闭

                        getV().chongdianzhuang(bean);
//                        Log.d("ssss", "获取到数据");
                    }

                    @Override
                    public void onError(Throwable e) {
                        getV().loadingComplete();
                        Log.d("ssss", "onError: "+e);
                    }

                    @Override
                    public void onComplete() {
                        getV().loadingComplete();
                        Log.d("ssss", "完成");
                    }
                });
    }

}
